// 主页面

import { Theme } from "../../models/theme";
import { Banner } from "../../models/banner";
import { Category } from "../../models/category";
import { Activity } from "../../models/activity";
import { SpuPageing } from "../../models/spu-pageing";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    themes: null, // 全部主题
    themeA: null, // 1
    themeE: null,
    themeF: null,
    themeH: null,
    themeESpu: null,
    bannerB: null,
    bannerG: null,
    categoryList: [],
    activityD: null,
    activityE: null,
    spuPaging: null,
    loadingType: "loading",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.initData();
    this.initBottomSpuList();
  },

  // 初始化瀑布流
  async initBottomSpuList() {
    // 初始化分页加载对象
    const paging = SpuPageing.getLatestPaging();
    this.data.spuPaging = paging;
    const data = await paging.getMoreData();
    if (!data) {
      return;
    }
    wx.lin.renderWaterFlow(data.items);
  },

  async initData() {
    // 获取全部主题 等待结果后执行后续操作
    const themes = new Theme();
    await themes.getThemes();

    // 1 获取顶部主题入口 A E 位置
    const themeA = await themes.getHomeLocationA();
    // 6 主题-单图
    const themeF = await themes.getHomeLocationF();
    // 5 滚动主题
    const themeH = await themes.getHomeLocationH();

    // 7 主题 SPU
    const themeE = await themes.getHomeLocationE();
    // 判断是否下架
    let themeESpu = [];
    if (themeE.online) {
      const data = await themes.getHomeLocationESpu();
      if (data && data.spu_list) {
        themeESpu = data.spu_list.slice(0, 8);
      }
    }

    // 2 Banner 滚动
    const bannerB = await Banner.getHomeLocationB();
    // 8 主题多图
    const bannerG = await Banner.getHomeLocationG();

    // 3 首页Menu菜单宫格
    const grid = await Category.getHomeLocationC();
    // 4 主题 - 单图优惠券活动
    const activityD = await Activity.getHomeLocationD();

    this.setData({
      themeA,
      themeE,
      themeF,
      themeH,
      themeESpu,
      bannerB,
      bannerG,
      grid,
      activityD,
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    const data = await this.data.spuPaging.getMoreData();
    if (!data) {
      // paging为null，
      return;
    }
    wx.lin.renderWaterFlow(data.items);
    if (!data.moreData) {
      this.setData({
        loadingType: "end",
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
